package com.xr.web.controller.miniprogram;

import com.xr.common.annotation.Log;
import com.xr.common.core.controller.BaseController;
import com.xr.common.core.domain.AjaxResult;
import com.xr.common.enums.BusinessType;
import com.xr.miniprogram.domain.MiniProgramService;
import com.xr.miniprogram.domain.vo.MiniProgramServiceVO;
import com.xr.miniprogram.service.IMiniProgramServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * description: 服务模块-详情接口
 *
 * @author: wls
 * @date: 2023/11/9 9:37
 * @version:1.0
 */
@Api("服务模块-详情")
@RestController
@RequestMapping("/mini/service")
public class MpServiceController extends BaseController {

    @Resource
    public IMiniProgramServiceService miniProgramServiceService;


    /**
     * 根据服务菜单编号获取服务详情
     * @param menuId
     * @return
     */
    @ApiOperation("根据服务菜单id获取服务详情")
    @PreAuthorize("@ss.hasPermi('mini:service:query')")
    @GetMapping(value = "/get")
    public AjaxResult get(@ApiParam(value = "菜单id", required = true) @RequestParam("menuId") Long menuId)
    {
        MiniProgramService service = miniProgramServiceService.getServiceByMenuId(menuId);
        if(service != null) return success(MiniProgramServiceVO.toMiniProgramServiceVO(service));
        else return success((Object) null);
    }

    /**
     * 新增服务详情
     * @param miniProgramServiceVO
     * @return
     */
    @ApiOperation("新增服务详情")
    @PreAuthorize("@ss.hasPermi('mini:service:add')")
    @Log(title = "服务详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MiniProgramServiceVO miniProgramServiceVO){
        miniProgramServiceVO.setCreateBy(getUsername());
        miniProgramServiceVO.setCreateTime(new Date());
        //设置版本号为 1
        miniProgramServiceVO.setServiceVersion(1L);
        return toAjax(miniProgramServiceService.addService(miniProgramServiceVO.toMiniProgramService()));
    }

    /**
     * 修改服务详情
     * @param miniProgramServiceVO
     * @return
     */
    @ApiOperation("修改服务详情")
    @PreAuthorize("@ss.hasPermi('mini:service:edit')")
    @Log(title = "服务详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody MiniProgramServiceVO miniProgramServiceVO){
        miniProgramServiceVO.setUpdateBy(getUsername());
        miniProgramServiceVO.setUpdateTime(new Date());
        //更新版本号
        miniProgramServiceVO.setServiceVersion(miniProgramServiceVO.getServiceVersion() + 1);
        return toAjax(miniProgramServiceService.updateService(miniProgramServiceVO.toMiniProgramService()));
    }
}
